<template>
  <div class="all">
    <div class="top">
      <el-row>
        <el-form ref="form" inline label-position="left">
          <el-col :span="3">
            <el-form-item label="编 号：">
              <el-input v-model="query.coding" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="名 称：">
              <el-input v-model="query.name" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="类 型：">
              <SelfSelect v-model="query.category" :showList="paramCategoryOption" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="状 态：">
              <SelfSelect v-model="query.status" :showList="status" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="单 位：">
              <el-input v-model="query.unit" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-button style="margin-top: 38%" icon="el-icon-search" type="primary" @click="index">查找</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-row class="btns">
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">新增</el-button>
        <el-button type="primary" icon="el-icon-printer">导出</el-button>
        <el-button type="primary" icon="el-icon-upload">批量新增</el-button>
        <el-button type="primary" icon="el-icon-upload">批量更新</el-button>
        <el-button type="primary" icon="el-icon-delete" @click="delBtn">删除</el-button>
        <el-button type="primary" icon="el-icon-download">下载模板</el-button>
        <el-button type="primary" icon="el-icon-refresh">刷新</el-button>
      </el-row>
      <span class="total">总数：{{this.total}}</span>
    </div>
    <div class="main">
      <div class="table">
        <el-table ref="multipleTable" v-loading="loading" border tooltip-effect="dark" width="100%" height="89%" :row-class-name="tableRowClassName" :data="data.dataList" @selection-change="handleSelectionChange">
          <el-table-column label="序号" type="index" align="center" width="50" />
          <el-table-column type="selection" align="center" width="55" />
          <el-table-column prop="coding" label="编号" align="center" width="150" />
          <el-table-column prop="name" label="名称" align="center" width="200" />
          <el-table-column prop="status" label="状态" align="center">
            <template scope="{ row }">
              <div v-if="row.status === 0" style="color: green">启用</div>
              <div v-else style="color: red">停用</div>
            </template>
          </el-table-column>
          <el-table-column prop="category" label="类型" align="center" :formatter="categoryFormatter" />
          <el-table-column prop="notice_low" label="预警偏低" align="center" />
          <el-table-column prop="warning_low" label="预警过低" align="center" />
          <el-table-column prop="notice_high" label="预警偏高" align="center" />
          <el-table-column prop="warning_high" label="预警过高" align="center" />
          <el-table-column prop="standard" label="标准" align="center" width="160" />
          <el-table-column label="操作" fixed="right" width="180" align="center">
            <template scope="{row}">
              <el-button type="primary" @click="goDetail(row.id)">编辑</el-button>
              <el-button type="danger" @click="handleDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination class="page" align="center" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="this.total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
      </div>
    </div>
  </div>
</template>

<script>
import API from '@/api/asset/param'
import { paramCategoryOption, status } from '@/utils/option'
import SelfSelect from '@/components/select-o'
export default {
  components: {
    SelfSelect
  },
  inject: ['goUrl', 'paramCategory'],
  data() {
    return {
      data: {
        dataList: []
      },
      total: 0,
      input: '',
      adminList: [],
      loading: true,
      positionTree: [],
      categoryTree: [],
      query: {
        limit: 15,
        page: 1
      },
      paramCategoryOption,
      status,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      categoryProps: {
        children: 'children',
        label: 'name'
      },
      multipleSelection: []
    }
  },
  created() {
    this.index()
  },
  methods: {
    index() {
      this.loading = true
      API.paramList(this.query).then(res => {
        this.data.dataList = res.data.data
        this.total = res.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    goDetail(id) {
      const url = `/param/edit/${id}`
      this.goUrl(url)
    },
    handleAdd() {
      this.goUrl('/param/add')
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'success-row'
      } else {
        return ''
      }
    },
    categoryFormatter(row) {
      return this.paramCategory(row)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete({ id }).then(res => {
          if (res.data.error_code !== 1) {
            this.$message.error(res.data.message)
          } else {
            this.$message.success('删除成功')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delBtn() {
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const idArray = []
        this.multipleSelection.forEach(item => {
          idArray.push(item.id)
        })
        const id = idArray.join(',')
        this.handleDel(id)
      } else {
        this.$message('请先选择，然后再删除')
      }
    }
  }
}
</script>

<style scoped>

</style>
